//
//  ZXClassifyViewController.m
//  XiMaFM
//
//  Created by fanzhuxian on 2017/9/6.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "ZXClassifyViewController.h"

#import "ZXCkassifyCollectionViewCell.h"
#import "ZXClassifyViewModel.h"

@interface ZXClassifyViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) ZXClassifyViewModel * classifyModel;

@property (nonatomic, strong) UICollectionView * collectionView;


@end

static NSString * const kIdentifier = @"classifyCell";

@implementation ZXClassifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];

    [self setupCollectionView];
    
    [self.classifyModel getDataCompletionHandle:^(NSError *error) {
        [self.collectionView reloadData];
    }];
}

#pragma mark - init CollectionView
- (void)setupCollectionView {

    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = -1;
    flowLayout.itemSize = CGSizeMake(DEVICE_SCREEN_WIDTH / 2, 40);
    flowLayout.headerReferenceSize = CGSizeMake(DEVICE_SCREEN_WIDTH, 10);
    
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT - 35 - 49 - 64) collectionViewLayout:flowLayout];
    
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    collectionView.backgroundColor =  [UIColor colorWithRed:239 / 255.0  green:243 / 255.0  blue:247 / 255.0 alpha:1];
    
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ZXCkassifyCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:kIdentifier];
    collectionView.bounces = YES;
    
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
}

#pragma mark - cllectionViewCell 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"%ld, %ld", indexPath.section, indexPath.row);
}

#pragma mark - collectionView DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 6;
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 5;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    ZXCkassifyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:kIdentifier forIndexPath:indexPath];
  
    [cell.imageView sd_setImageWithURL:[self.classifyModel coverURLForIndex:indexPath.row + (indexPath.section * 5)] placeholderImage:[UIImage imageNamed:@"cell_bg_noData_7"]];
    
    cell.titleLabel.text = [self.classifyModel titleForIndex:indexPath.row + (indexPath.section * 5)];
    
    cell.backgroundColor = [UIColor whiteColor];
    
    return  cell;
}

#pragma mark - private methods
- (CGFloat)getImageHightForwidth:(CGFloat)itemWidth
                    atIndexPath:(NSIndexPath *)indexPath{
    
    
    return 40;
}

#pragma mark - 懒加载
- (ZXClassifyViewModel *)classifyModel{
    if (!_classifyModel) {
        _classifyModel = [[ZXClassifyViewModel alloc] init];
    }
    return _classifyModel;
}

@end
